package com.cjl.web.backend;

import com.cjl.entity.EbUser;
import com.cjl.mapper.UserMapper;
import com.cjl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller //标记为web服务员；
@RequestMapping("/user")  //工号；
public class UserController {
    @Autowired    //spring会将UserService对象创建出来
    private UserService userService;

    /**
     * 将数据放到model中，将页面放到view中
     * spring mvc---将model渲染到view中
     * list ---分页请求数据
     *
     * @return
     */
    @RequestMapping("/list")
    /**
     * 获取用户分页信息
     */
    public String list(Model model,
                       @RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "7") int pageSize) {
        System.out.println("start service....." + page);
        //请求service层获取用户的分页数据
        final List<EbUser> userList = userService.listByPage(page, pageSize);
        final Integer totalPage = userService.findTotalPage(pageSize);
        //传递model（数据）和view视图（jsp）
        //将model和jsp整合  key-value 键值对
        model.addAttribute("userList", userList);
        //model绑定当前总页数
        model.addAttribute("totalPage", totalPage);
        //model绑定当前页
        model.addAttribute("currPage", page);
        return "manage/user";//视图 manage/user.jsp通过进行获取数据
    }

    //前缀： 由spring mvc框架 后缀：/WEB-INF/manage/user-add.jsp
    @RequestMapping("/to_save")
    public String toSave() {
        return "manage/user-add";
    }

    @RequestMapping("/save")
    public String save(EbUser ebUser) {
        //传入数据内容
        System.out.println("EbUser==>" + ebUser);
        //将生日的年月日组装成 生日
        Date birth = new Date(ebUser.getBirthYear() - 1900, ebUser.getBirthMonth() - 1, ebUser.getBirthDay());
        //将组装成的生日给他赋值
        ebUser.setEuBirthday(birth);
        //result返回受影响行数
        int result = userService.save(ebUser);
        System.out.println("result ==>" + result);
        //根据result的结果>0跳转页面
        if (result <= 0) {
            //TODO 添加失败后的处理 1.提示失败 2.抛出异常
            return "manage/result-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") String id) {
        int result = userService.delete(id);
        if (result == 0) {
        }
        return "manage/manage-result";
    }

    @RequestMapping("/to_edit")
    public String toEdit(Model model, @RequestParam("euUserId") String euUserId) {
        System.out.println("点击跳转正在进行...euUserId===" + euUserId);
        //调用Service层的方法查询点击的用户的所有信息
        EbUser ebUser = userService.selectOne(euUserId);
        //指定model和jsp
        model.addAttribute("ebUser", ebUser);
        return "manage/user-modify";
    }

    @RequestMapping("/edit")
    public String edit(EbUser ebUser) {
        //传入数据内容
        System.out.println("EbUser==>" + ebUser);
        //将生日的年月日组装成 生日
        Date birth = new Date(ebUser.getBirthYear() - 1900, ebUser.getBirthMonth() - 1, ebUser.getBirthDay());
        //将组装成的生日给他赋值
        ebUser.setEuBirthday(birth);
        //result返回受影响行数
        int result = userService.updateById(ebUser);
        if (result <= 0) {
            return "manage/result-error";
        }
        return "manage/manage-result";
    }
}